package com.ias.agdis_r.util.flight;

import com.ias.agdis_r.R;
import com.ias.agdis_r.manager.DataBaseManager;
import com.ias.agdis_r.util.S;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

/**
 * 与UI相关的工具
 * 
 * @author qibo.yu
 *
 */
public class UiUtil {

	private static final int LOADING_VIEW_ID = R.id.id_ui_progress_loading_data;
	private static final int LOADING_VIEW_LAYOUT_ID = R.layout.progress_view_loading_data;

	private static final int ACTIVITY_ROOT_VIEW_ID = android.R.id.content;

	private static final int DEFAULT_BG_COLOR = 0X00EEEEEE;
	private static final int DEFAULT_BG_COLOR2 = 0XFFFFFF;
	private static final String DEFAULT_MESSAGE = DataBaseManager.getInstance().getContext().getString(R.string.loading);

	/**
	 * 在Activity中添加一个View以提示正在加载数据(透明背景)
	 * 
	 * @param activity
	 * @return
	 */
	public static boolean showLoadingUi2(Activity activity) {
		return showLoadingUi(activity, DEFAULT_MESSAGE, Color.TRANSPARENT);
	}

	/**
	 * 在Activity中添加一个View以提示正在加载数据
	 * 
	 * @param activity
	 * @return
	 */
	public static boolean showLoadingUi(Activity activity) {
		return showLoadingUi(activity, DEFAULT_MESSAGE);
	}

	public static boolean showLoadingUi(Activity activity, String message) {
		return showLoadingUi(activity, message, DEFAULT_BG_COLOR);
	}

	/**
	 * 航班查询界面的不透明进度条
	 */
	public static boolean showFlightInfoLoadingUi(Activity activity){
		LayoutInflater inflater = LayoutInflater.from(activity);
		ViewGroup view = (ViewGroup) inflater.inflate(LOADING_VIEW_LAYOUT_ID, getActivityRootLayout(activity), true);
		((TextView) view.findViewById(R.id.loading_process_dialog_txt)).setText(DEFAULT_MESSAGE);
		view.findViewById(R.id.id_ui_progress_loading_data).setBackgroundColor(0XFFFFFFFF);
		return true;
	}

	/**
	 * 为指定的Activity显示进度条
	 * @param activity 需显示进度条的Activity
	 * @param message 提示文字
	 * @param backgroundColor 遮罩层背景色
	 * @return
	 */
	public static boolean showLoadingUi(Activity activity, String message, int backgroundColor){
		// TODO 子View屏蔽事件绑定
		LayoutInflater inflater = LayoutInflater.from(activity);
		ViewGroup view = (ViewGroup) inflater.inflate(LOADING_VIEW_LAYOUT_ID, getActivityRootLayout(activity), true);
		view.setBackgroundColor(backgroundColor);
		((TextView) view.findViewById(R.id.loading_process_dialog_txt)).setText(message);
		return true;
	}

	/**
	 * 如果进度条存在，移除之
	 * 
	 * @param activity
	 * @return执行移除返回true，否则返回false
	 */
	public static boolean removeLoadingUiIfExist(final Activity activity) {
		final View view = activity.findViewById(LOADING_VIEW_ID);
		if (view != null) {
			ObjectAnimator animator = ObjectAnimator.ofFloat(view, "alpha", 1f, 0f).setDuration(600);

			animator.addListener(new AnimatorListenerAdapter() {
				@Override
				public void onAnimationEnd(Animator animation) {
					getActivityRootLayout(activity).removeView(view);
					//在检查一次
					View v = activity.findViewById(LOADING_VIEW_ID);
					if(null != v){
						getActivityRootLayout(activity).removeView(v);
					}
					super.onAnimationEnd(animation);
				}
			});
			animator.start();
			return true;
		}
		return false;
	}

	/**
	 * 获取Activity的跟根视图Framlayout
	 * 
	 * @param activity
	 * @return
	 */
	public static FrameLayout getActivityRootLayout(Activity activity) {
		return (FrameLayout) activity.findViewById(ACTIVITY_ROOT_VIEW_ID);
	}

	private UiUtil() {
	}

}
